package com.nimitz.modules.demo.controller;

import com.nimitz.common.core.constant.UserConstants;
import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.demo.domain.DemoDepartment;
import com.nimitz.modules.demo.service.IDemoDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName DemoDepartmentController
 * @Author Vincent
 * @Date 2021-08-17-15:32
 * @PackageName com.nimitz.modules.demo.controller
 * @Description 演示模块-部门控制层
 * @Version 1.0.0
 */
@RestController
@Api(tags = "部门管理")
@RequestMapping("/demo/department")
public class DemoDepartmentController extends BaseController {

    /**
     * 测试框架正常运行使用，测试完毕后删除hello方法即可
     *
     * @return
     */
    @ApiOperation(value = "测试框架")
    @GetMapping("/hello")
    public String hello() {
        return "Hello Spring Boot";
    }

    @Autowired
    private IDemoDepartmentService demoDepartmentService;

    /**
     * 查询所有部门信息（含搜索）
     *
     * @param demoDepartment 部门信息
     * @return 部门信息集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有部门信息（含搜索）")
    public AjaxResult list(DemoDepartment demoDepartment) {
        List<DemoDepartment> demoDepartments = demoDepartmentService.selectDemoDepartmentList(demoDepartment);
        return AjaxResult.success(demoDepartments);
    }

    /**
     * 新增部门信息
     *
     * @param demoDepartment 部门信息
     * @return 结果
     */
    @PostMapping
    @ApiOperation(value = "新增部门信息")
    public AjaxResult add(@RequestBody DemoDepartment demoDepartment) {
        if (UserConstants.NOT_UNIQUE.equals(demoDepartmentService.checkDemoDepartmentNameUnique(demoDepartment))) {
            return AjaxResult.error("新增部门'" + demoDepartment.getDepartmentName() + "'失败，部门名称已存在");
        }
        //该处的创建者应该为登录用户的名称，暂时写死在这里。
        demoDepartment.setCreateBy("admin");
        return toAjax(demoDepartmentService.insertDemoDepartment(demoDepartment));
    }

    /**
     * 修改部门信息
     *
     * @param demoDepartment 部门信息
     * @return 结果
     */
    @PutMapping
    @ApiOperation(value = "修改部门信息")
    public AjaxResult edit(@RequestBody DemoDepartment demoDepartment) {
        if (UserConstants.NOT_UNIQUE.equals(demoDepartmentService.checkDemoDepartmentNameUnique(demoDepartment))) {
            return AjaxResult.error("修改部门'" + demoDepartment.getDepartmentName() + "'失败，部门名称已存在");
        }
        //该处的更新者应该为登录用户的名称，暂时写死在这里。
        demoDepartment.setUpdateBy("admin");
        return toAjax(demoDepartmentService.updateDemoDepartment(demoDepartment));
    }

    /**
     * 删除部门信息
     *
     * @param demoDepartmentId 部门id
     * @return 结果
     */
    @DeleteMapping("/{demoDepartmentId}")
    @ApiOperation(value = "删除部门信息")
    public AjaxResult remove(@PathVariable Long demoDepartmentId) {
        return toAjax(demoDepartmentService.deleteDemoDepartmentByDepartmentId(demoDepartmentId));
    }
}
